<template>
  <div class="app-container">
    <el-dialog :title="titles[flag]" :visible.sync="dialogVisible" width="30%" append-to-body>
      <el-form ref="form" :model="form" :disabled="flag==1">
        <el-form-item label="支付名称" prop="paymentName" required>
          <el-input v-model="form.paymentName" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="支付编码" prop="paymentCode" required>
          <el-input v-model="form.paymentCode" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="期初金额(元)" prop="original" required>
          <el-input type="number" v-model="form.original" min="0" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="是否启用" prop="enable" required>
          <el-switch v-model="form.enable" :active-value="0"
                     :inactive-value="1"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="dialogOk" v-if="flag!=1">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addPayType, updatePayType} from "../../../../api/payMng";

export default {
  data() {
    return {
      flag: 0,
      titles: ['新增', '详情', '修改'],
      loading: false,
      dialogVisible: false,
      form: {}
    }
  },
  methods: {
    show(flag, form) {
      this.flag = flag
      this.dialogVisible = true
      this.resetForm('form')
      this.form = form
    },
    dialogOk() {
      this.$refs.form.validate(e => {
        if (e) {
          if (!this.flag) {
            this.loading = true
            addPayType(this.form).then(res => {
              this.dialogVisible = false
              this.$message.success('新增成功')
              this.$emit('ok')
            }).finally(e => {
              this.loading = false
            })
          } else {
            this.loading = true
            updatePayType(this.form).then(res => {
              this.dialogVisible = false
              this.$message.success('修改成功')
              this.$emit('ok')
            }).finally(e => {
              this.loading = false
            })
          }
        }
      })
    },
  }
}
</script>
